package l8;

import java.util.Arrays;

public class SeqString {
    private char[] value;
    private int count;

    public SeqString(String str) {
        if (str == null) {
            str = "";
        }
        count = str.length();
        value = new char[count];
        for (int i = 0; i < count; i++) {
            value[i] = str.charAt(i);
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(value[i]);
        }
        return sb.toString();
    }

    public SeqString(SeqString s) {

        if (s == null) {
            s = new SeqString("");
        }
        count = s.count;
        value = new char[count];
        for (int i = 0; i < count; i++) {
            value[i] = s.value[i];
        }
    }

    public int length() {
        return count;
    }

    public char charAt(int index) {
        if (index < 0 || index >= count) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + count);
        }
        return value[index];
    }

    public SeqString subString(int start, int end) {
        if (start < 0) {
            start = 0;
        }
        if (end > count) {
            end = count;
        }
        if (start > end) {
            int temp = start;
            start = end;
            end = temp;
        }
        SeqString result = new SeqString("");
        result.count = end - start;
        result.value = new char[count];
        for (int i = 0; i < result.count; i++) {
            result.value[i] = value[start + i];
        }
        return result;
    }

    public int compareTo(SeqString anotherString) {
        int anotherCount = anotherString.count;
        int minCount = Math.min(count, anotherCount);
        for (int i = 0; i < minCount; i++) {
            char c1 = value[i];
            char c2 = anotherString.value[i];
            if (c1 != c2) {
                return c1 - c2;
            }
        }
        return count - anotherCount;
    }

    public SeqString replace(SeqString anotherString) {
        if (anotherString == null) {
            anotherString = new SeqString("");
        }
        count = anotherString.count;
        value = new char[count];
        for (int i = 0; i < count; i++) {
            value[i] = anotherString.value[i];
        }
        return this;
    }

    public int bfFind(SeqString sub, int pos) {
        int i=pos;
        int j=0;
        if (sub == null || sub.count == 0) {return -1;}
        if (count<sub.count){return -1;}

        while (i<count&&j<sub.count) {
            if (value[i] == sub.value[j]) {
                i++;j++;
            }
            else {
                i=i-j+1;
                j=0;
            }
        }
        if (j>=sub.count) {return i-sub.count;}
        else {return -1;}
    }

    public static void main(String[] args) {
        SeqString s1 = new SeqString("Hello, World!");
        SeqString s2 = new SeqString("Java");
        SeqString s3 = new SeqString("World");

        System.out.println("s1:"+s1.toString());
        System.out.println("s2:"+s2.toString());
        System.out.println("s3:"+s3.toString());

        System.out.println("s1的长度："+s1.length());
        System.out.println("s1和s2的比较结果是"+s1.compareTo(s2));
        System.out.println("s1的子串（从3开始，长度为5）:"+s1.subString(3,8).toString());

        System.out.println("在s1中查找s3的位置"+s1.bfFind(s3,0));
        System.out.println("在s1中查找s2的位置"+s1.bfFind(s2,0));
    }
}
